﻿using System;
using TheBigB.JsonSerializerLite;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tester
{
	/// 
	/// Serializer tests.
	/// 
	/// Runs the serializer against valid and invalid input.
	/// 
	/// Changelog:
	///		2012-04-29
	///			- Added child-object of different type
	///			
	///		2012-04-30
	///			- Added recursion detection test
	///			
	///		2012-05-01
	///			- Fixed recursion detection test
	///			- Added comments
	///			
	///		2012-05-04
	///			- Removing recursion detection until it's fixed
	///			
	///		2012-07-30
	///			- Removed recursion detection completely
	///

	/// <summary>
	/// Serializer tests.
	/// </summary>
	[TestClass]
	public class SerializationTests
	{
		/// <summary>
		/// Runs invalid input against the JsonSerializer and verifies expected output
		/// </summary>
		[TestMethod]
		public void TestInvalidInput()
		{
			string output, expected;

			int[] inputArray = new int[] { 1, 2, 3, 4 };
			expected = "{}";
			output = JsonSerializer.Default.Serialize(inputArray);
			Assert.AreEqual(expected, output, "The Serialize() method is not handling arrays as input as expected");

			int inputInt = 1;
			expected = "{}";
			output = JsonSerializer.Default.Serialize(inputInt);
			Assert.AreEqual(expected, output, "The Serialize() method is not handling value-types as input as expected");

			string inputStr = "test";
			expected = "{}";
			output = JsonSerializer.Default.Serialize(inputStr);
			Assert.AreEqual(expected, output, "The Serialize() method is not handling strings as input as expected");

			TestObject inputNullTestObject = null;
			expected = "null";
			output = JsonSerializer.Default.Serialize(inputNullTestObject);
			Assert.AreEqual(expected, output, "The Serialize() method is not handling null input as expected");
		}

		/// <summary>
		/// Runs valid input against the JsonSerializer and verifies expected output
		/// </summary>
		[TestMethod]
		public void TestValidInput()
		{
			TestObject input;
			string output, expected;

			// Test empty, initialized object
			input = new TestObject();
			expected = "{\"testIntArray\":[],\"testByte\":0,\"testShort\":0,\"testInt\":0,\"testLong\":0,\"testFloat\":0,\"testDouble\":0,\"testString\":\"\",\"embeddedObject\":null,\"childObject\":null}";
			output = JsonSerializer.Default.Serialize(input);
			Assert.AreEqual(expected, output, "The Serialize() method is not handling the empty test object properly");

			// Test filled object with empty initialized embedded object
			input = new TestObject();
			input.testIntArray = new int[] { 1, 2, 3, 4 };
			input.testByte = 1;
			input.testShort = 2;
			input.testInt = 3;
			input.testLong = 4;
			input.testFloat = 5.5F;
			input.testDouble = 6.6;
			input.testString = "abcd";
			input.embeddedObject = new TestObject();
			input.childObject = new TestChildObject();
			input.childObject.name = "xyz";
			expected = "{\"testIntArray\":[1,2,3,4],\"testByte\":1,\"testShort\":2,\"testInt\":3,\"testLong\":4,\"testFloat\":5.5,\"testDouble\":6.6,\"testString\":\"abcd\",\"embeddedObject\":{\"testIntArray\":[],\"testByte\":0,\"testShort\":0,\"testInt\":0,\"testLong\":0,\"testFloat\":0,\"testDouble\":0,\"testString\":\"\",\"embeddedObject\":null,\"childObject\":null},\"childObject\":{\"name\":\"xyz\"}}";
			output = JsonSerializer.Default.Serialize(input);
			Assert.AreEqual(expected, output, "The Serialize() method is not handling the filled test object properly");
		}
	}
}
